
#ifndef PCF8574_H_
#define PCF8574_H_

#include "RP6I2CmasterTWI.h"
#include <stdbool.h>

#define MAKE_PCF8574(PCF, ADDRESS)  PCF=createPCF8574(ADDRESS)

typedef union pcf8574_status_un{
	uint8_t byte;
	struct {
		unsigned P1:1;
		unsigned P2:1;
		unsigned P3:1;
		unsigned P4:1;
		unsigned P5:1;
		unsigned P6:1;
		unsigned P7:1;
		unsigned P8:1;
	};
} pcf8574_status_t;

typedef struct pcf8574_str pcf8574_t;


struct pcf8574_str{
	uint8_t address;
	uint8_t initialized;
	pcf8574_status_t status;
	uint8_t(*update)(pcf8574_t *pcf);
};

void updatePCF8574Status(pcf8574_t *pcf){
	I2CTWI_transmitByte(pcf->address,pcf->status.byte);
}

void initializePCF8574(pcf8574_t *pcf, uint8_t addressP){
	pcf->initialized=true;
	pcf->address=addressP;
	pcf->status.byte=255;
	pcf->update=&updatePCF8574Status;
}


pcf8574_t *createPCF8574(uint8_t addressP){
	pcf8574_t *ret=(pcf8574_t*)malloc(sizeof(pcf8574_t));
	initializePCF8574(ret, addressP);
	ret->update(ret);
	return ret;
}

#endif